package unit;

import junit.framework.TestCase;

import com.google.code.netcheckin.aprs.LatOrLong;
import com.google.code.netcheckin.io.Ascii;

public class ULatOrLong extends TestCase {

	public ULatOrLong(String name) {
		super(name);
	}

	public void testAAInstantiateDecimalDeg() {
		double decdeg = 39.5183;
		byte bytedir = 'N';
		int ambiguity = 0;
		LatOrLong lol = new LatOrLong(decdeg, bytedir, ambiguity);
		assertTrue("lat/long not latitude", lol.isLatitude());
		assertEquals("wrong latitude", decdeg, lol.getDecimalDeg());
		assertEquals("wrong direction", 'N', lol.getDirection());
	}

	public void testABInstantiateDegMin() {
		int degrees = 99;
		double minutes = 39.5183;
		byte bytedir = 'E';
		int ambiguity = 0;
		LatOrLong lol = new LatOrLong(degrees, minutes, bytedir, ambiguity);
		assertEquals("wrong latitude", 99.65864, lol.getDecimalDeg(), .0001);
		assertTrue("lat/long not longitude", lol.isLongitude());
		assertEquals("wrong direction", 'E', lol.getDirection());
	}

	public void testACGetAmibuityBytes() {
		String value = "4912.03N";
		assertEquals("N 0 wrong ambiguity", 0, LatOrLong.getAmbiguity(value.getBytes(), 0, value.length(), false));
		value = "4912.0 N";
		assertEquals("N 1 wrong ambiguity", 1, LatOrLong.getAmbiguity(value.getBytes(), 0, value.length(), false));
		value = "4912.  N";
		assertEquals("N 2 wrong ambiguity", 2, LatOrLong.getAmbiguity(value.getBytes(), 0, value.length(), false));
		value = "491 .  N";
		assertEquals("N 3 wrong ambiguity", 3, LatOrLong.getAmbiguity(value.getBytes(), 0, value.length(), false));
		value = "49  .  N";
		assertEquals("N 4 wrong ambiguity", 4, LatOrLong.getAmbiguity(value.getBytes(), 0, value.length(), false));
		value = "4   .  N";
		assertEquals("N 5 wrong ambiguity", 5, LatOrLong.getAmbiguity(value.getBytes(), 0, value.length(), false));
		value = " 4   .  N";
		assertEquals("N offset wrong ambiguity", 5, LatOrLong.getAmbiguity(value.getBytes(), 1, value.length(), false));
		value = "4912.03N ";
		assertEquals("N trail space  wrong ambiguity", 0, LatOrLong.getAmbiguity(value.getBytes(), 0, value.length() - 1, false));
		value = "10912.00W";
		assertEquals("W 0 wrong ambiguity", 0, LatOrLong.getAmbiguity(value.getBytes(), 0, value.length(), true));
		value = "10912.0 W";
		assertEquals("W 1 wrong ambiguity", 1, LatOrLong.getAmbiguity(value.getBytes(), 0, value.length(), true));
		value = "10912.  W";
		assertEquals("W 2 wrong ambiguity", 2, LatOrLong.getAmbiguity(value.getBytes(), 0, value.length(), true));
		value = "1091 .  W";
		assertEquals("W 3 wrong ambiguity", 3, LatOrLong.getAmbiguity(value.getBytes(), 0, value.length(), true));
		value = "109  .  W";
		assertEquals("W 4 wrong ambiguity", 4, LatOrLong.getAmbiguity(value.getBytes(), 0, value.length(), true));
		value = "10   .  W";
		assertEquals("W 5 wrong ambiguity", 5, LatOrLong.getAmbiguity(value.getBytes(), 0, value.length(), true));
		value = "1    .  W";
		assertEquals("W 6 wrong ambiguity", 6, LatOrLong.getAmbiguity(value.getBytes(), 0, value.length(), true));
	}

	public void testADInstantiatePacketBytes() {
	   try {

		String value = "912758.37S";
		LatOrLong lol = new LatOrLong(value.getBytes(), 2);
		assertEquals("S wrong latitude", 27.973, lol.getDecimalDeg(), .001);
		assertEquals("S wrong ambiguity", 0, lol.getAmbiguity());
		assertTrue("S not seen as latitude", lol.isLatitude());
		assertEquals("S wrong direction", 'S', lol.getDirection());

		value = "2758.3 N";
		lol = new LatOrLong(value.getBytes());
		assertEquals("N wrong latitude", 27.97167, lol.getDecimalDeg(), .0001);
		assertTrue("N not seen as latitude", lol.isLatitude());
		assertEquals("N wrong direction", 'N', lol.getDirection());
		assertEquals("N wrong ambiguity", 1, lol.getAmbiguity());

		value = "10758.3 E";
		lol = new LatOrLong(value.getBytes());
		assertEquals("E wrong latitude", 107.97167, lol.getDecimalDeg(), .0001);
		assertTrue("E not seen as longitude", lol.isLongitude());
		assertEquals("E wrong direction", 'E', lol.getDirection());
		assertEquals("E wrong ambiguity", 1, lol.getAmbiguity());

		value = "911758.75W";
		lol = new LatOrLong(value.getBytes(), 1);
		assertEquals("W wrong latitude", 117.9792, lol.getDecimalDeg(), .0001);
		assertTrue("W not seen as longitude", lol.isLongitude());
		assertEquals("W wrong direction", 'W', lol.getDirection());
		assertEquals("W wrong ambiguity", 0, lol.getAmbiguity());

	   } catch(Exception e) {
		e.printStackTrace();
		fail(e.getMessage());
	   }
	}

	public void testAEIndexOfDirectionString() {
		assertEquals("first wrong", 4, LatOrLong.indexOfDirection("ABCDEFG"));
		assertEquals("2nd  wrong", 0, LatOrLong.indexOfDirection("SBCDXFG"));
		assertEquals("3nd  wrong", 6, LatOrLong.indexOfDirection("ABCDXFW"));
		assertEquals("4th  wrong", 6, LatOrLong.indexOfDirection("ABCDXFNW"));
	}

	public void testAFInstantiateString() {
	   try {
		LatOrLong lol = new LatOrLong("186.731W");
		assertEquals("W wrong longitude", 186.731, lol.getDecimalDeg());
		assertTrue("W not seen as longitude", lol.isLongitude());
		assertEquals("W wrong direction", 'W', lol.getDirection());

		lol = new LatOrLong("86.666E");
		assertEquals("E wrong longitude", 86.666, lol.getDecimalDeg());
		assertTrue("E not seen as longitude", lol.isLongitude());
		assertEquals("E wrong direction", 'E', lol.getDirection());

		lol = new LatOrLong("39.100N");
		assertEquals("N wrong longitude", 39.1, lol.getDecimalDeg());
		assertTrue("N not seen as longitude", lol.isLatitude());
		assertEquals("N wrong direction", 'N', lol.getDirection());

		lol = new LatOrLong("9.756S");
		assertEquals("S wrong longitude", 9.756, lol.getDecimalDeg());
		assertTrue("S not seen as longitude", lol.isLatitude());
		assertEquals("S wrong direction", 'S', lol.getDirection());

	   } catch(Exception e) {
		e.printStackTrace();
		fail(e.getMessage());
	   }
	}

	public void testAGImposeAmbiguity() {
		String locats = "12317.72W";
		byte[] locat = locats.getBytes();
		LatOrLong.imposeAmbiguity(locat, 0);
		assertEquals("0 wrong", locats, new String(locat));
		
		locat = locats.getBytes();
		LatOrLong.imposeAmbiguity(locat, 1);
		assertEquals("1 wrong", "12317.7 W", new String(locat));

		locat = locats.getBytes();
		LatOrLong.imposeAmbiguity(locat, 2);
		assertEquals("2 wrong", "12317.  W", new String(locat));

		locat = locats.getBytes();
		LatOrLong.imposeAmbiguity(locat, 3);
		assertEquals("3 wrong", "1231 .  W", new String(locat));

		locat = locats.getBytes();
		LatOrLong.imposeAmbiguity(locat, 4);
		assertEquals("4 wrong", "123  .  W", new String(locat));

		locat = locats.getBytes();
		LatOrLong.imposeAmbiguity(locat, 5);
		assertEquals("5 wrong", "12   .  W", new String(locat));

	}

	public void testAHToPacketBytes() {
		LatOrLong lol = new LatOrLong(17, 21.76, Ascii.V_N, 0);
		byte[] result = lol.toPacketBytes();
		assertEquals("1 wrong length", 8, result.length);
		assertEquals("1 wrong data", "1721.76N", new String(result));

		lol = new LatOrLong(7, 21.76, Ascii.V_E, 2);
		result = lol.toPacketBytes();
		assertEquals("2 wrong length", 9, result.length);
		assertEquals("2 wrong data", "00721.  E", new String(result));
	}

}
